import type { ConfigEnv, UserConfig } from 'vite';
import { loadEnv } from 'vite';
// need install plugin @typings/node
import { resolve } from 'path';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
// import tailwindcss from '@tailwindcss/vite';
function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const root = process.cwd();

  const env = loadEnv(mode, root);

  // this function can be converted to different typings
  const { VITE_BASEURL } = env;

  const isBuild = command === 'build';

  return {
    base: './',
    server: {
      // Listening on all local ips
      host: true,
      open: true,
      port: 3060,
      // Load proxy configuration from .env
      proxy: {
        '/api': {
          target: VITE_BASEURL,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
        },
      },
    },

    // the project uses lots of vite plugins, so they are extracted and managed separately
    plugins: [vue(), vueJsx()],

    build: {
      target: 'es2015',
      cssTarget: 'chrome86',
      minify: 'terser',
      terserOptions: {
        compress: {
          keep_infinity: true,
          // used to delete console and debugger in production environment
          // drop_console: VITE_DROP_CONSOLE
        },
      },
      chunkSizeWarningLimit: 2000,
    },

    css: {
      preprocessorOptions: {
        less: {
          // charset: false,
          // additionalData: `@import "${resolve('src/design/variable/index.less')}";`,
          javascriptEnabled: true,
        },
      },
    },

    resolve: {
      alias: [
        {
          find: /@\//,
          replacement: pathResolve('src') + '/',
        },
      ],
    },
  };
};
